package com.example.eurekaconsumer.controller;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Date: 2021/2/19 10:57
 * @Author: yulei
 * @Description:
 */
@Controller
@RequestMapping("/Goods")
public class GoodsController {
    @Autowired
    private Environment env;

    @Autowired
    RestTemplate restTemplate;

    /**
     * 搜索结果列表
     *
     * @param keywords
     * @param page
     * @param size
     * @param model
     * @return
     */
    @RequestMapping("/search")
    public String search(@RequestParam("keywords") String keywords, @RequestParam("page") String page, @RequestParam("size") String size, Model model) {
        String re = restTemplate.getForObject("http://" + env.getProperty("producer.name") + "/search?keywords=" + keywords + "&page=" + page + "&size=" + size, String.class);
        List<HashMap<String, Object>> list = JSON.parseObject(re, List.class);
        model.addAttribute("title", "搜索列表");
        model.addAttribute("proInfo", list);
        return "pro_list";

    }

    /**
     * 商品详情*****
     *
     * @param goodsId
     * @param model
     * @return
     */
    @GetMapping("/pro_info/{goodsId}")
    public String pro_info(@PathVariable("goodsId") String goodsId, Model model) {
        Map<String, String> map = new HashMap<>();
        map.put("goodsId", goodsId);
        //getForObject 传参规则，可点击查看源码************************
        Map proInfo = restTemplate.getForObject("http://" + env.getProperty("producer.name") + "/Goods/pro_info?goodsId={goodsId}", Map.class, map);
        model.addAttribute("title", "商品详情");
        model.addAttribute("proInfo", proInfo);
        return "pro_info";
    }

    /**
     * 商品列表页面
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/pro_list")
    public String pro_list(HttpServletRequest request, Model model) {
        //商品列表
//        List list=sysUserService.getList(request.get);
//        model.addAttribute("keywords",(request.getParameter("keywords"))==null?"":request.getParameter("keywords"));
//        model.addAttribute("title","列表");
//        model.addAttribute("goods",list);
        return "pro_list";
    }

}
